package servlet;


import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/UpdateUserInfoServlet.do")
public class update extends HttpServlet {
    // SearchUserServlet.do
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        int  user_id = Integer.parseInt(request.getParameter("id"));
        int state=request.getIntHeader("state");
        int role=request.getIntHeader("role");
        String gender = request.getParameter("Gender");
        String profession = request.getParameter("Profession");
        String description=request.getParameter("description");
        String[] favor = request.getParameterValues("Favor"); // 获取多个参数值
        String favorString;
        if (favor != null && favor.length > 0) {
            // 将多个参数值拼接成一个字符串，以逗号分隔拼接多个值
            favorString = String.join(",", favor);
        } else {
            favorString = ""; // 如果未获取到任何参数值，可以设置为空字符串或其他默认值
        }
        boolean registrationSuccess = register(state,role, gender, profession, favorString,description,user_id);
        if (registrationSuccess) {
            // 注册成功，重定向到注册成功页面
            response.sendRedirect("usermanager/success.jsp");
        } else {
            // 注册失败，重定向到错误页面
            response.sendRedirect("usermanager/errorPage.jsp");
        }
    }

    public boolean register(int state, int  role, String gender, String profession, String favor, String description ,int user_id) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql = "UPDATE 02_38_yangyang SET state = ? and role=? and  gender=? and profession=? and favor=?  description=?  WHERE user_id = ?";

        // 执行插入操作，传入参数并获取操作结果
        boolean registrationSuccess = jdbcUtil.update(sql,state ,role , gender, profession, favor, description ,user_id);

        // 返回操作结果
        return registrationSuccess;
    }

    private List<Map<String, Object>> performSearch(int userid) {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE user_id = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, userid);

            while (resultSet.next()) {
                String username=resultSet.getString("username");
                int id = resultSet.getInt("user_id");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");
                int state = resultSet.getInt("state");
                int role = resultSet.getInt("role");

                // 检查是否存在 null 值，避免空指针异常
                if (gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "id", id,
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description,
                            "state", state,
                            "role", role
                    );


                    // 将用户信息Map添加到列表中
                    userList.add(user);
                }
            }
            // 处理数据库异常
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

        return userList;
    }

    private List<Map<String, Object>> allquiry() {
        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        try {
            String sql = "SELECT * FROM 02_38_yangyang"; // 假设数据库表名为 users
            ResultSet resultSet = jdbcUtil.query(sql);

            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String gender = resultSet.getString("gender");
                String profession = resultSet.getString("profession");
                String favor = resultSet.getString("favor");
                String description = resultSet.getString("description");

                // Check for null values before adding to the map
                if (username != null && password != null && gender != null && profession != null && favor != null && description != null) {
                    Map<String, Object> user = Map.of(
                            "username", username,
                            "gender", gender,
                            "profession", profession,
                            "favor", favor,
                            "description", description
                    );

                    userList.add(user);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 处理数据库异常
            // 可以跳转到错误页面或返回错误信息
        } finally {
            jdbcUtil.closeConnection();
        }
        return  userList;
    }
}


